<?php
/*
 *   CakePHP PagSeguro Plugin
 *   Copyright (C) 2009  Yuri Pereira Teixeira - (blog.yuriteixeira.com)
 *   Based on Felipe Theodoro Gonçalves's original work (http://code.google.com/p/pagseguro/)
 *
 *   This program is free software: you can redistribute it and/or modify
 *	 it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
class PagSeguroComponent extends Object {

	/**
	 * Versão do plugin
	 * @var String
	 */
	public $version = '0.1';

	/**
	 * array padrão
	 * @var array
	 */
	private $sale = array(
		'email_cobranca' => null,
		'tipo' => 'CP',
		'moeda' => 'BRL',
		'ref_transacao' => null,
		'tipo_frete' => 'SD',
		'produto' => array(
			1 => array(
				'item_id' => null,
				'item_descr' => null,
				'item_valor' => null,
				'item_quant' => null,
				'item_frete' => null,
				'item_peso' => null,
				'item_quant' => null,
			)
		),
		'cliente' => array(
			'nome' => null,
			'cep' => null,
			'end' => null,
			'num' => null,
			'compl' => null,
			'bairro' => null,
			'cidade' => null,
			'uf' => null,
			'pais' => null,
			'ddd' => null,
			'tel' => null,
			'email' => null
		)
	);

	/**
	 * Seta os dados
	 * @param array $data = null
	 * @return boolean
	 */
	public function setSale($data = null){
		if(is_null($data)){
			return false;
		}elseif(is_array($data)){
			$this->sale = $data;
			return true;
		}else{
			return false;
		}
	}

	/**
	 * Organiza o array para envio ao helper
	 * @return array
	 */
	public function generateInformation(){
		if(empty($this->sale)){
			return false;
		}else{
			$data = array();
			foreach($this->sale as $key => $value){

				if($key == 'produto'){
					if(is_array($value)){
						foreach($value as $key2 => $val){
							foreach($val as $key3 => $product){
								$data[$key3 . '_' . $key2] = $product;
							}
						}
					}
				}elseif($key == 'cliente' && is_array($value)){
					foreach($value as $key4 => $custumer){
						$data[$key . '_' . $key4] = $custumer;
					}
				}else{
					$data[$key] = $value;
				}
			}
			return $data;
		}
	}
}